import { contextBridge, ipcRenderer } from 'electron'
import path from 'path'
import { FileStatus } from '@/types';

contextBridge.exposeInMainWorld('electron', {
  openDirectory: () => ipcRenderer.invoke('dialog:openDirectory'),
  analyzeExcel: (filePath: string) => ipcRenderer.invoke('excel:analyze', filePath),
  mergeExcel: (filePath: string, files: FileStatus[]) => ipcRenderer.invoke('excel:merge', filePath, files),
  joinPath: (...paths: string[]) => path.join(...paths),
  getAppVersion: () => ipcRenderer.invoke('app:getVersion'),
  // 分成两个独立函数
  addMergeProgressListener: (callback: (message: string) => void) => {
    const channel = 'excel:merge-progress';
    const listener = (_: any, message: string) => callback(message);
    ipcRenderer.on(channel, listener);
    // 返回监听器以便后续移除
    return listener;
  },
  removeMergeProgressListener: (listener: (...args: any[]) => void) => {
    const channel = 'excel:merge-progress';
    ipcRenderer.removeListener(channel, listener);
  }
})